import 'package:core_package/core_package.dart';
import 'package:core_package/ui/widget/my_asset_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/model/my_device_list_model.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:flutter_app_unionad/ui/user_center/change_user_device_info_page.dart';

// 我的设备
class UserCenterItem2Page extends StatefulWidget {
  static const String routeName = "/userCenterItem2Page";

  @override
  State<UserCenterItem2Page> createState() => _UserCenterItem2PageState();
}

class _UserCenterItem2PageState extends State<UserCenterItem2Page> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getLoad();
  }

  @override
  Widget build(BuildContext context) {
    return BaseNavigationWidget(
      title: '我的设备',
      child: buildContent(),
      needScrollView: false,
    );
  }

  List<MyDeviceListModel>? dataSource;

  void getLoad() async {
    dataSource = await FacilityRequest.getMyUserDeviceList();
    setState(() {});
  }

  Widget buildContent() {
    if (dataSource == null) {
      return ProgressDialog.showFirstLoading();
    }
    if (dataSource!.length == 0) {
      return Center(child: EmptyWidget());
    }
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemCount: dataSource!.length,
      itemBuilder: (context, index) {
        return Column(
          children: [
            GestureDetector(
              onTap: () {
                NavigatorUtil.push(context, ChangeUserDeviceInfoPage.routeName, arguments: dataSource![index]).then((value) => getLoad());
              },
              child: buildItemWidget(dataSource![index]),
            ),
            SizedBox(height: 10.px),
          ],
        );
      },
    );
  }

  Widget buildItemWidget(MyDeviceListModel model) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 20.px, horizontal: 20.px),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.px),
      ),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                builtItem(
                  image: 'take_info.png',
                  title:  '${model.devId}',
                  imageSize: Size(16, 13),
                ),
                SizedBox(
                  height: 5.px,
                ),
                builtItem(
                    image: 'take_address.png',
                    title: '${model.devProvince}${model.devCity}${model.devCounty}${model.devAddress}',
                    imageSize: Size(14, 16.5)),
                SizedBox(
                  height: 5.px,
                ),
                builtItem(
                  image: 'icon_iphone.png',
                  title: '${model.contractName}   ${model.contractTel!.replaceRange(3, 7, " **** ")}',
                  imageSize: Size(8, 13),
                ),
              ],
            ),
          ),
          SizedBox(width: 10.px),
          Icon(
            Icons.article_outlined,
            size: 20.px,
            color: MyColors.formHex("#1D242CFF"),
          )
        ],
      ),
    );
  }

  Widget builtItem({
    String? image,
    String? title,
    Size? imageSize,
    String? detail,
    Color? detailColor,
  }) {
    return Row(
      children: [
        Container(
          width: 13.px,
          alignment: Alignment.center,
          child: MyAssetImage(
            image ?? "",
            width: imageSize!.width.px,
            height: imageSize.height.px,
            fit: BoxFit.fill,
          ),
        ),
        MySizeBox(width: 15),
        Expanded(
          child: Text(
            "$title",
            style: TextStyle(
              color: MyColors.formHex("#1D242CFF"),
              fontSize: MyFont.font13,
              fontWeight: MyFontWeight.regular,
            ),
          ),
        ),
      ],
    );
  }
}
